﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using OTS.BLL;

public partial class WebControl_ProCookie : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindProCookie();
    }

    //输出之前浏览过的商品
    public void BindProCookie()
    {
        ProductBLL productBLL = new ProductBLL();
        DataTable dt = new DataTable();
        string strUrl = "";
        string strImgUrl = "";
        if (Request.Cookies["pro"] != null)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            //读取proID的内容
            string proStr = Request.Cookies["pro"].Values["proID"].ToString();
            //读取times的内容
            string times = Request.Cookies["pro"].Values["times"].ToString();

            if (proStr.Split(',').Length > 0 & times.Split(',').Length > 0) // & proNo.Split(',').Length > 0 & pic.Split(',').Length > 0 & price.Split(',').Length > 0 & title.Split('/').Length > 0
            {
                string[] pro = proStr.Split(',');
                string[] timesStr = times.Split(',');
                int count = pro.Length;

                if (count > 5)
                {
                    count = 5;
                }
                for (int i = 0; i < count; i++)
                {
                    dt = productBLL.GetAllProduct("where proID=" + pro[i]);
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        sb.Append("<div class='clearfix'>");
                        sb.Append("<div class='span-2 goodpic'>");
                        sb.AppendFormat("<a title='{0}{1}' target='_blank' href='product.aspx?proID={2}'>", dt.Rows[j]["title"].ToString(), dt.Rows[j]["proNO"].ToString(), dt.Rows[j]["proID"].ToString());
                        strUrl = dt.Rows[j]["bigImg_pic"].ToString().Replace("\\", "/");
                        strImgUrl = strUrl.Replace("~/", "");
                        sb.AppendFormat("<img width='46' height='70' src='{0}' />", strImgUrl);
                        sb.Append("</a>");
                        sb.Append("</div>");
                        sb.Append("<div class='prepend-2 goodsName'>");
                        //sb.Append("<div class='view-time'>");
                        //sb.AppendFormat("{0}的浏览过：", timesStr[i]);
                        //sb.Append("</div>");
                        sb.AppendFormat("<a title='{0}{1}' target='_blank' href='product.aspx?proID={2}'>{3}{4}", dt.Rows[j]["title"].ToString(), dt.Rows[j]["proNO"].ToString(), dt.Rows[j]["proID"].ToString(), dt.Rows[j]["title"].ToString(), dt.Rows[j]["proNO"].ToString());
                        sb.Append("</a>");
                        sb.Append("</div>");
                        sb.Append("</div>");
                        sb.Append("<hr />");
                        this.box_widgets.InnerHtml = sb.ToString();
                    }
                }
            }
        }
        else
        {
            this.lblMsg.Text = "暂无浏览过的商品";
            this.Panel2.Visible = false;
        }
    }

    //清空cookie
    protected void lbtnClear_Click(object sender, EventArgs e)
    {
        if (Request.Cookies["pro"] != null)
        {
            HttpCookie cookies = new HttpCookie("pro");
            cookies.Expires = DateTime.Now.AddDays(-1);
            Response.Cookies.Add(cookies);
        }
        int typeID = Convert.ToInt32(Request.QueryString["typeID"].ToString());
        Response.Redirect("gallery-grid.aspx?typeID=" + typeID);
    }
}
